package com.itheima.reggie.common;


import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;

@Component
@RestControllerAdvice
@Slf4j
public class GlobalExeptionHandler{

    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public R hardException(SQLIntegrityConstraintViolationException e){
        e.printStackTrace();
        String message = e.getMessage();
        String[] split = message.split("'");
        return R.error("用户"+split[1]+"已存在");
    }



//处理我们的自定义业务异常
    @ExceptionHandler(BusinessException.class)
    public R handlerBusinessException(BusinessException exception){
        log.error(exception.getMessage());
        return R.error(exception.getMessage());
    }

}
